var aFields=new Array();
var aErrMsgs=new Array();
var oAllInputs;

function fecCheckRequiredFields (oForm){		
	var aErrors=new Array();
	var aMissmatch=new Array();
	var aList=new Array();	
	var sAdditionalExplanation=new String();
	oAllInputs=oForm.getElementsByTagName('INPUT');	
	for(i=0; i<aFields.length; i++) {		
		if (aFields[i][2]!='' && (typeof(oForm[aFields[i][0]])=='object' || aFields[i][1]=='checkbox')) {
			if (typeof(aFields[i][4])=='undefined' || (typeof(aFields[i][4])!='undefined' && aFields[i][4]==oForm.name)){
				switch(aFields[i][1]) {
					case "text":
						if (oForm[aFields[i][0]].value=='' || oForm[aFields[i][0]].value==' ') {
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];			
						}			
						break;					
					case "textarea":
						if (oForm[aFields[i][0]].value=='' || oForm[aFields[i][0]].value==' ') {
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];			
						}			
						break;
					case "password":
						if (oForm[aFields[i][0]].value=='' || oForm[aFields[i][0]].value==' ') {
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];			
						} else {
							if (fecCheckRequiredPassword (aFields[i],oForm)==false) {
								aMissmatch[aMissmatch.length]=new Array();
								aMissmatch[aMissmatch.length-1]=aFields[i];
							}					
						}
						break;
					case "select":
						if (oForm[aFields[i][0]].value=='' || oForm[aFields[i][0]].value==' ') {
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];			
						}			
						break;
					case "radio":			
						if(fecCheckRequiredRadiobutton (aFields[i],oForm)==false){
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];		
						};
						break;
					case "checkbox":					
						if(fecCheckRequiredCheckbox (aFields[i],oForm)==false){
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];		
						};
						break;
					case "url":
						if (oForm[aFields[i][0]].value=='' || oForm[aFields[i][0]].value==' ') {
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];			
						}			
						break;
					case "email":
						if (oForm[aFields[i][0]].value=='' || oForm[aFields[i][0]].value==' ') {
							aErrors[aErrors.length]=new Array();
							aErrors[aErrors.length-1]=aFields[i];			
						} else {
							if (oForm[aFields[i][0]].value.match(/^[a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])+@[a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])+\.[a-zA-Z]{2,4}/)==null) {
							//if (oForm[aFields[i][0]].value.match(/^[\w\.\-]+\@{1}[\w\.\-]+\.{1,}\w{2,10}/)==null) {
								aMissmatch[aMissmatch.length]=new Array();
								aMissmatch[aMissmatch.length-1]=aFields[i];
							}									
						}
						
						break;
					case "file":
						break;
				}		
			}
		}
	}	
	if (aErrors.length<1) {
		if (aMissmatch.length<1) {
			oForm.submit();
		} else {
			for (i=0; i<aMissmatch.length; i++)	{
				aList[aList.length]=aMissmatch[i][3];	
				if (aMissmatch[i][1]=='password' && sAdditionalExplanation=='') sAdditionalExplanation+=aErrMsgs[2];				
			}
			alert(aErrMsgs[1]+" "+aList.join(', ')+". "+sAdditionalExplanation);
		}
	} else {			
		alert(aErrMsgs[0]);
	}
}

function fecRegisterFormElement (sName,sType,sCheck,sLabel,sFormname) {
	if (sName!='' && sType!='' && sLabel!='') {
		var i=aFields.length;
		aFields[i]=new Array();
		aFields[i][0]=sName;
		aFields[i][1]=sType;
		aFields[i][2]=sCheck;
		aFields[i][3]=sLabel;	
		aFields[i][4]=sFormname;
	}
}
function fecRegisterErrorMessages (sErrMsgEmptyFields, sBadForm, sPasswordsDoNotMatch){
	aErrMsgs[0]=sErrMsgEmptyFields;
	aErrMsgs[1]=sBadForm;
	aErrMsgs[2]=sPasswordsDoNotMatch;
}
function fecCheckRequiredCheckbox (aCB,oForm){
	var bHasAtLeastOneChecked=new Boolean(false);
	for(iii in oAllInputs) {
		if (oAllInputs[iii].type=='checkbox') {
			if (oAllInputs[iii].name.substr(0,aCB[0].length)==aCB[0] && oAllInputs[iii].checked==true)	bHasAtLeastOneChecked=true; 
		}
	}
	return bHasAtLeastOneChecked;
}
function fecCheckRequiredRadiobutton (aRadio,oForm){
	var bHasAtLeastOneChecked=new Boolean(false);
	for(ii=0; ii<oForm[aRadio[0]].length; ii++) {
		if (oForm[aRadio[0]][ii].value!=undefined && oForm[aRadio[0]][ii].checked==true) {
			bHasAtLeastOneChecked=true; 
		} else {
			
		}
	}
	return bHasAtLeastOneChecked;
}
function fecCheckRequiredPassword (oPw,oForm){
	for(iii=0; iii<aFields.length; iii++) {
		if (aFields[iii][1]=='password' && oForm[aFields[iii][0]].value!=oForm[oPw[0]].value) return false;	
	}
	return true;
}